var token = localStorage.getItem("key")


function updMobile() {
	if(null == document.getElementById("mobile").value || '' == document.getElementById("mobile").value) {
		mui.toast("请输入您的手机号");
		return;
	}
	if(null == document.getElementById("code").value || '' == document.getElementById("code").value) {
		mui.toast("请输入您的验证码");
		return;
	}
	if(!verifyMobile(document.getElementById("mobile").value)){
		mui.toast("请输入3-16位手机号");
		return
	}
	
	var mobile = $("#mobile").val();
	var code = $("#code").val();
	showDialog();
	$.ajax({
		type: "POST",
		url: url_prefix + "user/edit/updateUserMobile",
		dataType: 'JSON',
		data: {
			request:JSON.stringify({
				mobile: mobile,
				code: code,
				token: token
			})
		},
		success: function(data) {
			dismissDialog();
			if(data.code == '200') {
				var loginData = JSON.parse(localStorage.getItem("LoginData"))
				loginData.mobile = mobile
				localStorage.setItem('LoginData', JSON.stringify(loginData));
				window.location.href = "home.html?bool=true";
			} else if (data.code == '300'){
				mui.toast("帐号在其他设备上登录")
				window.location.href="fastLogin.html"
			} else {
				mui.toast(data.message);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {

			// if(textStatus=='timeout'){
			dismissDialog();
			mui.toast("连接服务器失败,请稍后重试``````");
			//  }
		}
	});
}

function verifyMobile(val) {
	var p1 = /^[0-9]{3,16}$/;  
    var me = false;  
    if (p1.test(val)) {
    	me=true;
    }
    return me
}

function getcode() {
	if(null == document.getElementById("mobile").value || '' == document.getElementById("mobile").value) {
		mui.toast("请输入您的手机号码");
		return;
	}
	var phone = $("#mobile").val();
	
	var me = verifyMobile(phone)
	
	if(me){
		$.ajax({
			type: "POST",
			url: url_prefix + "user/edit/send_code",
			dataType: 'JSON',
			data: {
				request: JSON.stringify({
					mobile: phone,
					token: token
				})
			},
			success: function(data) {
				if(data.code =='200'){
					settime();
				} else if (data.code == '300'){
					mui.toast("帐号在其他设备上登录")
					window.location.href="fastLogin.html"
				} else {
					mui.toast(data.message)
				}
				
			}
		});
	}else{
		mui.toast('请输入3-16位手机号')
	}
}

var countdown = 60;

function settime() {
	if(countdown == 0) {
		var val = document.getElementById('messgecode');
		val.removeAttribute("disabled");
		document.getElementById('messgecode').innerHTML = "获取验证码";
		//          val.value="获取验证码";
		countdown = 60;
	} else {
		var val = document.getElementById('messgecode');
		val.setAttribute("disabled", true);
		document.getElementById('messgecode').innerHTML = "重新发送(" + countdown + ")";
		//          val.value="重新发送(" + countdown + ")";
		countdown--;
		setTimeout(function() {
			settime();
		}, 1000)
	}
}

function clearMobile(){
	$("#mobile").val("")
}
